package com.example.demo.service.impl;

import com.example.demo.domain.entity.OperaLog;
import com.example.demo.domain.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public Integer login(String name, String passWord) {

        log.info("login , name: {}, password: {}", name, passWord);
        return userMapper.login(name, passWord);
    }

    @Override
    @Async
    public void insertTestData() {
        log.info("thread name: {}", Thread.currentThread().getName());
        Random random = new Random( 10000);
        for (int i = 0; i <200000; i++) {
            OperaLog data = OperaLog.builder()
                    .userId(i)
                    .ip("192.168.24.104")
                    .opData(random.nextInt())
                    .attr1(String.valueOf(random.nextInt()))
                    .attr2(String.valueOf(random.nextInt()))
                    .attr3(String.valueOf(random.nextInt()))
                    .attr4(String.valueOf(random.nextInt()))
                    .attr5(String.valueOf(random.nextInt()))
                    .attr6(String.valueOf(random.nextInt()))
                    .attr7(String.valueOf(random.nextInt()))
                    .attr8(String.valueOf(random.nextInt()))
                    .attr9(String.valueOf(random.nextInt()))
                    .attr10(String.valueOf(random.nextInt()))
                    .attr11(String.valueOf(random.nextInt()))
                    .attr12(String.valueOf(random.nextInt()))
                    .build();
            userMapper.insertTestData(data);
        }
    }

    @Override
    public List<User> findUserInfo() {
        return userMapper.findUserInfo();
    }
}
